//
//  KLSettingTableViewController.m
//  KL
//
//  Created by mac on 15/12/21.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

#import "KLSettingTableViewController.h"
#import "KLSetting.h"
#import <SVProgressHUD.h>
#import "KLAboutController.h"

@interface KLSettingTableViewController () <UIAlertViewDelegate, UIActionSheetDelegate>
/** 数据源 */
@property (nonatomic, strong) NSArray *data;

@end

@implementation KLSettingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.title = @"设置";
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0];
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    // 取出模型
    KLSetting *setting = self.data[indexPath.row];
    cell.textLabel.text = setting.title;
    cell.imageView.image = [UIImage imageNamed:setting.icon];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
    if (indexPath.row == 0) { // 评分支持
        [self support];
    }else if (indexPath.row ==1) { // 联系我们
        [self contactUs];
    }else if (indexPath.row ==2) { // 关于麻花
        [self aboutMahua];
    }else if (indexPath.row ==3) { // 清理内存
        [self cleanMemery];
    }
    
}

// 评分支持
#warning 在上架之前回来填空APPLE ID
- (void)support {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"给我们的应用评分" message:@"现在就去吗?" delegate:self cancelButtonTitle:@"以后吧" otherButtonTitles:@"去评分", nil];
    [alertView show];
}

- (void)contactUs {
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"现在联系?" delegate:self cancelButtonTitle:@"以后吧" destructiveButtonTitle:@"马上联系" otherButtonTitles:nil, nil];
    [sheet showFromTabBar:self.tabBarController.tabBar];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    KLLog(@"%ld", buttonIndex);
    if (buttonIndex) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://18201432664"]];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    KLLog(@"%ld", buttonIndex);
    if (buttonIndex) { // 拨打电话
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=1069899258"]];
    }
}

- (void)aboutMahua {
    KLAboutController *about = [[KLAboutController alloc] init];
    [self.navigationController pushViewController:about animated:YES];
}

- (void)cleanMemery {
    // 弹出提示框提示用户正在清理
    [SVProgressHUD showWithStatus:@"正在清理中..."];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSString *filePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSFileManager *mgr = [NSFileManager defaultManager];
        NSError *error = nil;
        if ([mgr fileExistsAtPath:filePath]) {  // 路径下有缓存
            long long memerySize = [mgr attributesOfItemAtPath:filePath error:&error].fileSize;
            [mgr removeItemAtPath:filePath error:&error];
            [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"成功清理了%.3fKB内存!", memerySize/1024.0]];
        }else { // 路径没有缓存
            [SVProgressHUD showInfoWithStatus:@"您的手机没有垃圾!"];
        }
        
    });
}

#pragma mark - 懒加载
- (NSArray *)data {
    if (!_data) {
        _data = [KLSetting settings];
    }
    return _data;
}

@end
